
<!DOCTYPE html
  PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   
      <title>1.8.&nbsp;使用 Python 的交互 Shell</title>
      <link rel="stylesheet" href="../diveintopython.css" type="text/css">
      <link rev="made" href="mailto:f8dy@diveintopython.org">
      <meta name="generator" content="DocBook XSL Stylesheets V1.52.2">
      <meta name="keywords" content="Python, Dive Into Python, tutorial, object-oriented, programming, documentation, book, free">
      <meta name="description" content="Python from novice to pro">
      <link rel="home" href="../toc/index.html" title="Dive Into Python">
      <link rel="up" href="index.html" title="第&nbsp;1&nbsp;章&nbsp;安装 Python">
      <link rel="previous" href="source.html" title="1.7.&nbsp;从源代码安装 Python ">
      <link rel="next" href="summary.html" title="1.9.&nbsp;小结">
   </head>
   <body>
      <table id="Header" width="100%" border="0" cellpadding="0" cellspacing="0" summary="">
         <tr>
            <td id="breadcrumb" colspan="5" align="left" valign="top">导航：<a href="../index.html">起始页</a>&nbsp;&gt;&nbsp;<a href="../toc/index.html">Dive Into Python</a>&nbsp;&gt;&nbsp;<a href="index.html">安装 Python</a>&nbsp;&gt;&nbsp;<span class="thispage">使用 Python 的交互 Shell</span></td>
            <td id="navigation" align="right" valign="top">&nbsp;&nbsp;&nbsp;<a href="source.html" title="上一页: “从源代码安装 Python ”">&lt;&lt;</a>&nbsp;&nbsp;&nbsp;<a href="summary.html" title="下一页: “小结”">&gt;&gt;</a></td>
         </tr>
         <tr>
            <td colspan="3" id="logocontainer">
               <h1 id="logo"><a href="../index.html" accesskey="1">深入 Python :Dive Into Python 中文版</a></h1>
               <p id="tagline">Python 从新手到专家 [Dip_5.4b_CPyUG_Release]</p>
            </td>
            <td colspan="3" align="right">
               <form id="search" method="GET" action="http://www.google.com/custom">
                  <p><label for="q" accesskey="4">Find:&nbsp;</label><input type="text" id="q" name="q" size="20" maxlength="255" value=""> <input type="submit" value="搜索"><input type="hidden" name="domains" value="woodpecker.org.cn/diveintopython"><input type="hidden" name="sitesearch" value="www.woodpecker.org.cn/diveintopython"></p>
               </form>
            </td>
         </tr>
      </table>
      <!--#include virtual="/inc/ads" -->
      <div class="section" lang="zh_cn">
         <div class="titlepage">
            <div>
               <div>
                  <h2 class="title"><a name="install.shell"></a>1.8.&nbsp;使用 <span class="application">Python</span> 的交互 Shell
                  </h2>
               </div>
            </div>
            <div></div>
         </div>
         <div class="abstract">
            <p>既然我们已经安装了 <span class="application">Python</span>，那么我们运行的这个交互 shell 是什么东西呢？
            </p>
         </div>
         <p><span class="application">Python</span> 扮演着两种角色。首先它是一个脚本解释器，可以从命令行运行脚本，也可以在脚本上双击，像运行其他应用程序一样。它还是一个交互 shell，可以执行任意的语句和表达式。这一点对调试、快速组建和测试相当有用。我甚至知道一些人把 <span class="application">Python</span> 的交互 shell 当作计算器来使用！
            
         </p>
         <p>在您的计算机平台上启动 <span class="application">Python</span> 的交互 shell，接下来让我们尝试着做些操作：
         </p>
         <div class="example"><a name="d0e3963"></a><h3 class="title">例&nbsp;1.5.&nbsp;初次使用交互 Shell</h3><pre class="screen">
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">1 + 1</span>               <a name="install.shell.1.1"></a><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12">
<span class="computeroutput">2</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput"><span class='pykeyword'>print</span> <span class='pystring'>'hello world'</span></span> <a name="install.shell.1.2"></a><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12">
<span class="computeroutput">hello world</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">x = 1</span>               <a name="install.shell.1.3"></a><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12">
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">y = 2</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">x + y</span>
<span class="computeroutput">3</span>
</pre><div class="calloutlist">
               <table border="0" summary="Callout list">
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#install.shell.1.1"><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left"><span class="application">Python</span> 的交互 shell 可以计算任意的 <span class="application">Python</span> 表达式，包括任何基本的数学表达式。
                     </td>
                  </tr>
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#install.shell.1.2"><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left">交互 shell 可以执行任意的 <span class="application">Python</span> 语句，包括 <span><b class="command">print</b></span> 语句。
                     </td>
                  </tr>
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#install.shell.1.3"><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left">也可以给变量赋值，并且变量值在 shell 打开时一直有效 (一旦关毕交互 Sheel，变量值将丢失)。</td>
                  </tr>
               </table>
            </div>
         </div>
      </div>
      <table class="Footer" width="100%" border="0" cellpadding="0" cellspacing="0" summary="">
         <tr>
            <td width="35%" align="left"><br><a class="NavigationArrow" href="source.html">&lt;&lt;&nbsp;从源代码安装 Python </a></td>
            <td width="30%" align="center"><br>&nbsp;<span class="divider">|</span>&nbsp;<a href="index.html#install.choosing" title="1.1.&nbsp;哪一种 Python 适合您？">1</a> <span class="divider">|</span> <a href="windows.html" title="1.2.&nbsp;Windows 上的 Python">2</a> <span class="divider">|</span> <a href="macosx.html" title="1.3.&nbsp;Mac OS X 上的 Python ">3</a> <span class="divider">|</span> <a href="macos9.html" title="1.4.&nbsp;Mac OS 9 上的 Python ">4</a> <span class="divider">|</span> <a href="redhat.html" title="1.5.&nbsp;RedHat Linux 上的 Python ">5</a> <span class="divider">|</span> <a href="debian.html" title="1.6.&nbsp;Debian GNU/Linux 上的 Python ">6</a> <span class="divider">|</span> <a href="source.html" title="1.7.&nbsp;从源代码安装 Python ">7</a> <span class="divider">|</span> <span class="thispage">8</span> <span class="divider">|</span> <a href="summary.html" title="1.9.&nbsp;小结">9</a>&nbsp;<span class="divider">|</span>&nbsp;
            </td>
            <td width="35%" align="right"><br><a class="NavigationArrow" href="summary.html">小结&nbsp;&gt;&gt;</a></td>
         </tr>
         <tr>
            <td colspan="3"><br></td>
         </tr>
      </table>
      <div class="Footer">
         <p class="copyright">Copyright © 2000, 2001, 2002, 2003, 2004 <a href="mailto:mark@diveintopython.org">Mark Pilgrim</a></p>
         <p class="copyright">Copyright © 2001, 2002, 2003, 2004, 2005, 2006, 2007 <a href="mailto:python-cn@googlegroups.com">CPyUG (邮件列表)</a></p>
      </div>
   </body>
</html>